package edu.umb.cs.antmanager.client;

import static edu.umb.cs.antmanager.common.Constants.CODE_BASE;
import static edu.umb.cs.antmanager.common.Constants.SECURITY_FILE;
import static edu.umb.cs.antmanager.common.Constants.AGENT_PORT;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

/**
 * Initialize the registry, setting property java.security.policy and
 * java.rmi.server.codebase ,if there is no <tt>SecurityManager</tt> create one.
 * if the registry do not exist, create a new one.
 * 
 * @author Zachary
 * 
 */
public class RMIRegistryInitializer {
	/**
	 * initialize registry.
	 */
	public static void init() {
		// it's required for RMI, which defiles the security policy for the
		// server
		System.setProperty("java.security.policy", SECURITY_FILE);
		// Required by RMI, used to find the server's java class
		System.setProperty("java.rmi.server.codebase", CODE_BASE);
		// if there is not security manager, create one.
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new SecurityManager());
		}
		try {
			// Try to create a new registry.
			LocateRegistry.createRegistry(AGENT_PORT);
		} catch (RemoteException e) {
			// There is a running Registry on that port, don't need
			// to create another one, just need to bind the agent
		}
	}

}
